package pl.edu.mimuw.irs.rcp.controller.editor;

import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import pl.edu.mimuw.irs.core.model.design.processing.Resource;
import pl.edu.mimuw.irs.core.model.design.processing.Role;
import pl.edu.mimuw.irs.rcp.controller.editor.input.ResourceEditorInput;
import pl.edu.mimuw.irs.rcp.controller.editor.util.RolesTableWrapper;
import pl.edu.mimuw.irs.rcp.util.MessageCode;
import pl.edu.mimuw.irs.rcp.util.UITexts;

public class ResourceEditor extends EditorPart {
	
	public static final String ID = "pl.edu.mimuw.irs.editor.resource";
	public static final String COMMAND_ID = "pl.edu.mimuw.irs.command.editresource";

	// *** Attributes
	
	private Resource resource;
	private Map<String, Role> rolesMap;
	private Label lblName;
	private Text txtName;
	private Label lblRoles;
	private RolesTableWrapper tblRoles;
	public Resource getResource() {
		return resource;
	}
	public void setResource(Resource resource) {
		this.resource = resource;
	}
	public Map<String, Role> getRolesMap() {
		if (rolesMap == null) rolesMap = new LinkedHashMap<String, Role>();
		return rolesMap;
	}
	public void setRolesMap(Map<String, Role> rolesMap) {
		this.rolesMap = rolesMap;
	}
	
	// *** Methods

	@Override
	public void doSave(IProgressMonitor monitor) {
		// XXX : container editor save

	}

	@Override
	public void doSaveAs() {
		// XXX : Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
		setResource(((ResourceEditorInput) input).getResource());
		setRolesMap(((ResourceEditorInput) input).getRolesMap());
		setPartName(getResource().getName());
	}

	@Override
	public boolean isDirty() {
		if (getResource().getName().equals(txtName.getText())) {
			return false;
		}
		return true;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// XXX save allowed
		return true;
	}

	@Override
	public void createPartControl(Composite parent) {
		GridData gridData;
		Composite composite =  new Composite(parent, SWT.NULL);

	    // create the desired layout for this wizard page
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;	
		
		lblName = new Label(composite, SWT.NONE);
		lblName.setText(UITexts.get(MessageCode.RESOURCE_EDITOR_NAME));				
		txtName = new Text(composite, SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		//gridData.horizontalSpan = ncol - 1;
		txtName.setLayoutData(gridData);
		txtName.setText(getResource().getName());
		
		gridData = new GridData();
		lblRoles = new Label(composite, SWT.NONE);
		lblRoles.setText(UITexts.get(MessageCode.RESOURCE_EDITOR_CONTENTS));
		gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
		tblRoles = new RolesTableWrapper(composite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL, 
				getResource().getRolesInstances());
		tblRoles.setLayoutData(gridData);
		
		
	}
	
	@Override
	public void setFocus() {
	}
}
